package service.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@EnableAutoConfiguration
@EnableFeignClients(clients = HelloWorldService.class)
@RestController
public class ServiceClientApplication {

    private static final Logger log = LoggerFactory.getLogger(ServiceClientApplication.class);

    @Autowired
    private HelloWorldService helloWorldService;

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/index")
    public String index() {
        String message = helloWorldService.helloWorld();
        log.info(message);
        return message;
    }

    @RequestMapping("/rest")
    public String rest() {
        String message =  restTemplate.getForObject("http://service-provider/hello/world", String.class);
        log.info(message);
        return message;
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(ServiceClientApplication.class)
                .run("--spring.config.additional-location=classpath:/META-INF/service-client.yaml");

    }
}
